﻿using System.Collections.Generic;
using System.Web.Mvc;
using Service.Permission;

namespace CMS.Areas.EasyUI.Controllers
{
    /// <summary>
    /// TreeBoxController
    /// </summary>
    public class TreeBoxController : BaseController
    {
        private readonly UserActionService _useractionservice;

        private readonly UserMenuService _usermenuservice;

        /// <summary>
        /// TreeBoxController
        /// </summary>
        public TreeBoxController(UserActionService useractionservice, UserMenuService usermenuservice)
        {
            _useractionservice = useractionservice;
            _usermenuservice = usermenuservice;
        }

        /// <summary>
        /// ActionTree
        /// </summary> 
        public ActionResult ActionTree(int id)
        {
            ViewBag.UID = id;
            return View();
        }

        /// <summary>
        /// MenuTree
        /// </summary> 
        public ActionResult MenuTree(int id)
        {
            ViewBag.UID = id;
            return View();
        }

        /// <summary>
        /// 获取Action TreeJson
        /// </summary> 
        [HttpGet]
        public JsonResult GetActionTreeJson(int uid)
        {
            var data = _useractionservice.ManagerTree(uid);

            foreach (var item in data)
            {
                if (item.children == null || item.children.Count <= 0) continue;
                foreach (var children in item.children)
                {
                    if (!children.@checked)
                        item.@checked = false;
                }
            }

            return new JsonResult() { Data = data, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        /// <summary>
        /// 获取Menu TreeJson
        /// </summary> 
        [HttpGet]
        public JsonResult GetMenuTreeJson(int uid)
        {
            var list = _usermenuservice.ManagerTree(uid);

            if (list != null && list.Count > 0)
                foreach (var item in list)
                {
                    if (item.children == null || item.children.Count <= 0) continue;
                    foreach (var children in item.children)
                    {
                        if (!children.@checked)
                            item.@checked = false;
                    }
                }

            return new JsonResult() { Data = list, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        /// <summary>
        /// 更新操作权限树
        /// </summary>
        /// <param name="list"></param> 
        /// <param name="UserID"></param> 
        [HttpPost, ValidateAntiForgeryToken]
        public JsonResult UpdateUserAction(List<Model.Permission.UserAction> list, int UserID)
        {
            return Json(_useractionservice.UpdateTree(list, UserID) ? new { code = 200, msg = "更新成功！" } : new { code = 401, msg = "更新失败！" });
        }

        /// <summary>
        /// 更新菜单权限树
        /// </summary>
        /// <param name="list"></param> 
        /// <param name="UserID"></param> 
        [HttpPost, ValidateAntiForgeryToken]
        public JsonResult UpdateUserMenu(List<Model.Permission.UserMenu> list, int UserID)
        {
            return Json(_usermenuservice.UpdateTree(list, UserID) ? new { code = 200, msg = "更新成功！" } : new { code = 401, msg = "更新失败！" });
        }

        /// <summary>
        /// 添加操作权限
        /// </summary> 
        public JsonResult AddUserAction(Model.Permission.UserAction model)
        {
            return Json(_useractionservice.Insert(model) > 0 ? new { code = 200, msg = "添加成功！" } : new { code = 401, msg = "添加失败！" });
        }
    }
}